package com.rp.system.controller;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.codehaus.jackson.JsonParseException;
import org.codehaus.jackson.map.JsonMappingException;
import org.quartz.SchedulerException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.rp.common.utils.JsonUtil;
import com.rp.common.utils.RpUtil;
import com.rp.common.vo.PageData;
import com.rp.controller.RpController;
import com.rp.system.po.Task;
import com.rp.system.po.User;
import com.rp.system.service.TaskService;

@Controller
@RequestMapping("/rp/system/taskMng/")
public class TaskController extends RpController {

	@Autowired
	private TaskService taskService;
	
	/*@ResponseBody
	@RequestMapping("getTasks")
	public PageData getTasks(){
		return new PageData(taskService.getTaskDao().getTasksByPage(pageParam));
	}
	
	@ResponseBody
	@RequestMapping("addTask")
	public String addTask(String json){
		System.out.println(json);
		Map<String, Object> obj = JsonUtil.jsonToMap(json);
		
		//判断任务名称是否已存在
		if(taskService.checkTaskNameExist((String)obj.get("taskName"))){
			return "任务名已存在";
		}else{
			Task task = new Task();
			task.setTaskName((String)obj.get("taskName"));
			task.setJobClass((String)obj.get("jobClass"));
			task.setStartTime(((String)obj.get("startTime")).replace("T", " "));
			task.setEndTime(((String)obj.get("endTime")).replace("T", " "));
			task.setRepeatCount((Integer) obj.get("repeatCount"));
			
			Integer intervalTime = (Integer) obj.get("intervalTime");
			intervalTime = intervalTime * 60000;
			task.setIntervalTime(intervalTime.toString());
			
			task.setCreateDate(RpUtil.getCurrentDatetime());
			try {
				taskService.addTask(task);
			} catch (SchedulerException e) {
				e.printStackTrace();
				return e.getMessage();
			} catch (ClassNotFoundException e) {
				e.printStackTrace();
				return "无法找到路径为（"+e.getMessage()+"）的类";
			}
		}
		return SUCCESS;
	}
	
	*//**
	 * 启动调度器
	 * @return
	 *//*
	@ResponseBody
	@RequestMapping("startDdq")
	public String startDdq(){
		try {
			taskService.start();
		} catch (SchedulerException e) {
			e.printStackTrace();
		}
		return SUCCESS;
	}
	
	*//**
	 * 关闭调度器
	 * @return
	 *//*
	@ResponseBody
	@RequestMapping("closeDdq")
	public String closeDdq(){
		try {
			taskService.shutdown();
		} catch (SchedulerException e) {
			e.printStackTrace();
		}
		return SUCCESS;
	}
	
	
	@ResponseBody
	@RequestMapping("changeTask")
	public String changeTask(String json, String operate){
		try {
			List<Task> tasks = JsonUtil.getMapper().readValue(json, JsonUtil.getCollectionType(ArrayList.class, Task.class));
			for (Task task : tasks) {
				if(operate.equals("pause")){
					taskService.pauseTask(task.getTaskName());
				}else if(operate.equals("recover")){
					taskService.recoverTask(task.getTaskName());
				}else if(operate.equals("delete")){
					taskService.deleteTask(task.getTaskName());
				}
			}
		} catch (JsonParseException e) {
			e.printStackTrace();
		} catch (JsonMappingException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} catch (SchedulerException e) {
			e.printStackTrace();
		}
		return SUCCESS;
	}*/
	
	
}
